using System;
using System.Collections;
using System.Threading;

using SdlDotNet.Graphics;
using SdlDotNet.Core;
using SdlDotNet.Input;

namespace Audible
{
    public class JoystickButtonEvent
    {
        protected int button;
        public int Button
        {
            get { return button; }
        }
        public JoystickButtonEvent(int Button)
        {
            button = Button;
        }
    }
    public class JoystickButtonsEvent
    {
        byte[] buttons;
        public byte[] Buttons
        {
            get { return buttons; }
        }
        public JoystickButtonsEvent(byte[] Buttons)
        {
            buttons = Buttons;
        }
    }
    public class JoystickMovedEvent
    {
        float pos;
        public float AxisPos
        {
            get { return pos; }
        }
        public JoystickMovedEvent(float axisPos)
        {
            pos = axisPos;
        }
    }

    /// <summary>
    /// Summary description for JoystickManager.
    /// </summary>
    public class JoystickManager
    {
        bool quitThread = false;
        Joystick joystick;
        Thread t;

        // events
        public delegate void JotstickButtonDownDelegate(object sender, JoystickButtonEvent e);
        public event JotstickButtonDownDelegate JoystickButtonDown;
        protected virtual void OnJoystickButtonDown(JoystickButtonEvent e)
        {
            if (JoystickButtonDown != null)
                JoystickButtonDown(this, e);
        }
        public delegate void JotstickButtonUpDelegate(object sender, JoystickButtonEvent e);
        public event JotstickButtonUpDelegate JoystickButtonUp;
        protected virtual void OnJoystickButtonUp(JoystickButtonEvent e)
        {
            if (JoystickButtonUp != null)
                JoystickButtonUp(this, e);
        }
        public delegate void JoystickMovedDelegate(object sender, JoystickMovedEvent e);
        public event JoystickMovedDelegate JoystickMovedHorz;
        protected virtual void OnJoystickMovedHort(JoystickMovedEvent e)
        {
            if (JoystickMovedHorz != null)
                JoystickMovedHorz(this, e);
        }
        public event JoystickMovedDelegate JoystickMovedVirt;
        protected virtual void OnJoystickMovedVirt(JoystickMovedEvent e)
        {
            if (JoystickMovedVirt != null)
                JoystickMovedVirt(this, e);
        }

        /*
         *
         */
        public JoystickManager()
        {
            t = new Thread(new ThreadStart(DoWork));
            t.Start();
        }

        public void Stop()
        {
            quitThread = true;
        }

        ~JoystickManager()
        {
            Stop();
        }

        void Events_Tick(object sender, TickEventArgs e)
        {
            if (quitThread)
                Events.QuitApplication();
        }

        void Events_JoystickButtonDown(object sender, JoystickButtonEventArgs e)
        {
            OnJoystickButtonDown(new JoystickButtonEvent(e.Button));
        }

        void Events_JoystickButtonUp(object sender, JoystickButtonEventArgs e)
        {
            OnJoystickButtonUp(new JoystickButtonEvent(e.Button));
        }

        void Events_JoystickAxisMotion(object sender, JoystickAxisEventArgs e)
        {
            if (e.AxisIndex == 0)
                OnJoystickMovedHort(new JoystickMovedEvent(joystick.GetAxisPosition(JoystickAxis.Horizontal)));
            else if (e.AxisIndex == 1)
                OnJoystickMovedVirt(new JoystickMovedEvent(joystick.GetAxisPosition(JoystickAxis.Vertical)));
        }

        private void DoWork()
        {
            Events.TargetFps = 10;
            Events.Tick += new EventHandler<TickEventArgs>(Events_Tick);
            Events.JoystickButtonDown += new EventHandler<JoystickButtonEventArgs>(Events_JoystickButtonDown);
            Events.JoystickButtonUp += new EventHandler<JoystickButtonEventArgs>(Events_JoystickButtonUp);
            Events.JoystickAxisMotion += new EventHandler<JoystickAxisEventArgs>(Events_JoystickAxisMotion);
            try
            {
                joystick = Joysticks.OpenJoystick(0);

                Video.WindowIcon();
                Events.Run();
            }
            catch
            { }
        }

        public float GetVAxisPos()
        {
            return joystick.GetAxisPosition(JoystickAxis.Vertical);
        }

        public float GetHAxisPos()
        {
            return joystick.GetAxisPosition(JoystickAxis.Horizontal);
        }
    } 
}
